package com.dmp.pushlogs.bolt;

import com.dmp.common.date.DateUtils;
import com.dmp.storm.bolt.BaseMapBolt;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import static com.dmp.storm.base.BaseMapKey.MAPKEY_RESULTMAP_BOLTMSG;


/**
 * @ClassName CustomBolt
 * @Description 本地化字段处理
 *
 * @author sunzy
 * @date 2016年4月14日 下午4:37:42 
 * @version 1.0 
 * 
 */
public class ClickCustomBolt extends BaseMapBolt {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3372732981391364586L;
	private String fieldFrom;
	private String fieldTo;

	@Override
	public void prepare() {
		if (fieldTo == null) {
			fieldTo = fieldFrom;
		}
	}

	@Override
	public Map<String, Object> process(Map<String, Object> map) {
		Map<String, Object> record = map;

		// ua转换
		if (StringUtils.isNotBlank(fieldFrom)) {
			if (record != null && record.size() > 0) {
				String value = (String) record.get(fieldFrom);
				String newValue = parseUserAgent(value);

				record.put(fieldTo, newValue);
			}
		}

		// 日期转换  dd/MMM/yyyy:HH:mm:ss Z   -> yyyyMMddHHmmss
		//String logTime = (String) map.get("fe_time");
		//Date logDate;
		//try {
		//logDate = DateUtils.parseDate(logTime, Locale.ENGLISH,
		//"dd/MMM/yyyy:HH:mm:ss Z");
					//logTime = DateUtils.formatDate(logDate, "yyyyMMddHHmmss");
		//} catch (ParseException e) {
		//System.err.println((String) map.get(MAPKEY_RESULTMAP_BOLTMSG));
		//e.printStackTrace();
		//return null;
		//}
		//record.put("fe_time", logTime);
		
		// 日期转换 dd/MMM/yyyy:HH:mm:ss Z -> yyyyMMddHHmmss
		String logTime = (String) map.get("fe_time");
		long time = Long.parseLong(logTime);
		Date logDate = new Date(time);
		logTime = DateUtils.formatDate(logDate, "yyyyMMddHHmmss");
		record.put("fe_time", logTime);
		
		// 毫秒-> yyyyMMddHHmmss
		String adpushTimeStr = (String) map.get("close_time");
		long adpushTimeL = Long.valueOf(adpushTimeStr).longValue();
		Date adpushDate = new Date(adpushTimeL);
		adpushTimeStr = DateUtils.formatDate(adpushDate, "yyyyMMddHHmmss");
		record.put("close_time", adpushTimeStr);
		
		// 其它时间
		record.put("hour", DateUtils.formatDate(logDate, "yyyyMMddHH"));
		record.put("hour_h", DateUtils.formatDate(logDate, "HH"));
		record.put("day", DateUtils.formatDate(logDate, "yyyyMMdd"));
		record.put("day_d", DateUtils.formatDate(logDate, "dd"));
		record.put("month", DateUtils.formatDate(logDate, "yyyyMM"));
		record.put("month_m", DateUtils.formatDate(logDate, "MM"));
		
		//日志时间
		record.put("log_date", DateUtils.formatDate(adpushDate, "yyyy/MM/dd HH:mm:ss"));
		
		return record;
	}
	
	public static void main(String...args){
		long adpushTimeL = System.currentTimeMillis();
		Date adpushDate = new Date(adpushTimeL);
		System.out.println(DateUtils.formatDate(adpushDate, "yyyy/MM/dd HH:mm:ss"));
//		String logTime = "01/Oct/2016:04:13:23 +0800";
//		try {
//			Date logDate = DateUtils.parseDate(logTime, Locale.ENGLISH,
//					"dd/MMM/yyyy:HH:mm:ss Z");
//			logTime = DateUtils.formatDate(logDate, "yyyyMMddHHmmss");
//			System.out.println(logTime);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
	}

	private String parseUserAgent(String userAgentString) {
		String platformType = "other";

		if (StringUtils.isBlank(userAgentString))
			return platformType;

		UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);

		if (DeviceType.COMPUTER.equals(userAgent.getOperatingSystem()
				.getDeviceType())) {
			platformType = "pc";
		} else if (StringUtils.containsIgnoreCase(userAgentString, "android")) {
			platformType = "android";
		} else if (StringUtils.containsIgnoreCase(userAgentString, "iphone")) {
			platformType = "apple";
		}

		return platformType;
	}

	public void setFieldFrom(String fieldFrom) {
		this.fieldFrom = fieldFrom;
	}

	public void setFieldTo(String fieldTo) {
		this.fieldTo = fieldTo;
	}

}
